package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.enums.ResultEnums;
import com.dongdongshop.pojo.TbGoods;
import com.dongdongshop.pojo.TbGoodsDesc;
import com.dongdongshop.pojo.TbItem;
import com.dongdongshop.pojo.TbSeller;
import com.dongdongshop.service.TbGoodsService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping("TbGoods")
public class TbGoodsController {

    //注入controller
    @DubboReference
    private TbGoodsService tbGoodsService;

    //添加商品(三表添加)
    @ResponseBody
    @RequestMapping("addTbGoods")
    public Result addTbGoods(TbGoods tbGoods, TbGoodsDesc tbGoodsDesc, TbItem tbItem) {
        TbSeller principal = (TbSeller) SecurityUtils.getSubject().getPrincipal();
        tbGoods.setSellerId(principal.getSellerId());
        tbItem.setCreateTime(new Date());
        tbItem.setUpdateTime(new Date());
        tbItem.setSeller(principal.getBankName());
        tbItem.setSellerId(principal.getSellerId());
        try {
            tbGoodsService.add(tbGoods, tbGoodsDesc, tbItem);
            return Result.response(ResultEnums.ADD_SUCCESS);
        } catch (Exception e) {
            System.out.println(e);
            return Result.response(ResultEnums.ADD_ERROR);
        }
    }
}
